package com.xiaomage.view.draw15

import android.content.Context
import android.graphics.*
import android.util.Log
import android.view.MotionEvent
import android.view.View
import android.R.attr.y
import android.R.attr.x
import android.widget.FrameLayout
import android.widget.LinearLayout
import android.widget.TextView
import android.graphics.Canvas.ALL_SAVE_FLAG
import android.graphics.RectF




/**
 * Created by Administrator on 2018/1/15.
 */
class MyView(context: Context):FrameLayout(context) {
    private var radius=50f
    private var startPointF=PointF(100f,100f)
    private var endPointF=PointF(0f,0f)
    private var paint=getPaint()
    private var mtouch=false
    private var path=Path()
    private var tv= TextView(context)
    init {
        setLayerType(View.LAYER_TYPE_SOFTWARE,null)
        val parmas=LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.WRAP_CONTENT,
        LinearLayout.LayoutParams.WRAP_CONTENT
        )
        tv.layoutParams=parmas
        tv.setPadding(10,10,10,10)
        tv.setTextColor(Color.parseColor("#ffffff"))
        tv.setBackgroundColor(Color.RED)
        tv.text="99+"
        addView(tv)

    }

    //子控件布局绘制
    override fun dispatchDraw(canvas: Canvas) {

        canvas.saveLayer(RectF(0f, 0f, width.toFloat(), height.toFloat()),
                paint, Canvas.ALL_SAVE_FLAG)
        if(mtouch){
            calculatePath()
            canvas.drawCircle(100f,100f,radius,paint)

            canvas.drawCircle(endPointF.x,endPointF.y,radius ,paint)
            canvas.drawPath(path,paint)


            tv.x=endPointF.x-tv.width/2
            tv.y=endPointF.y-tv.height/2
        }else{
            tv.x=startPointF.x-tv.width/2
            tv.y=startPointF.y-tv.height/2
        }
        canvas.restore();
        //这一句一定要放在最后，不然tv会被圆形覆盖掉
        super.dispatchDraw(canvas)

    }
    //当前布局绘制
    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)

    }

    override fun onTouchEvent(event: MotionEvent): Boolean {
        when(event.action){
            MotionEvent.ACTION_DOWN->{
                //判断触电是否在tv中
                val rect=Rect()
                var intArray=IntArray(2)
                tv.getLocationOnScreen(intArray)
                rect.left=intArray[0]
                rect.top=intArray[1]
                rect.right=tv.width+intArray[0]
                rect.bottom=tv.height+intArray[1]
                if(rect.contains(event.rawX.toInt(),event.rawY.toInt())){
                    mtouch=true
                }
            }
            MotionEvent.ACTION_UP->{
                mtouch=false
            }
        }
        endPointF.set(event.x,event.y)
        postInvalidate()
        return true
    }
    private fun calculatePath(){
      val a=Math.atan(((endPointF.y-startPointF.y)/(endPointF.x-startPointF.x)).toDouble())
        val x1=startPointF.x-radius*Math.sin(a)
        val y1=startPointF.y+radius*Math.cos(a)


        val x2=endPointF.x-radius*Math.sin(a)
        val y2=endPointF.y+radius*Math.cos(a)

        val x3=endPointF.x+radius*Math.sin(a)
        val y3=endPointF.y-radius*Math.cos(a)

        val x4=startPointF.x+radius*Math.sin(a)
        val y4=startPointF.y-radius*Math.cos(a)

        val centerX=(endPointF.x+startPointF.x)/2
        val centerY=(endPointF.y+startPointF.y)/2

        path.reset()
        path.moveTo(x1.toFloat(),y1.toFloat())
        path.quadTo(centerX,centerY,x2.toFloat(),y2.toFloat())
        path.lineTo(x3.toFloat(),y3.toFloat())
        path.quadTo(centerX,centerY,x4.toFloat(),y4.toFloat())
        path.lineTo(x1.toFloat(),y1.toFloat())
    }

    private fun getPaint():Paint{
        val paint=Paint();
        paint.style=Paint.Style.FILL_AND_STROKE
        paint.isAntiAlias=true
        paint.color= Color.RED
        return paint
    }
}